//
//  FXNetworkTools.swift
//  Swift网络工具抽取
//
//  Created by V on 15/12/30.
//  Copyright © 2015年 V. All rights reserved.
//

import UIKit
import AFNetworking


enum FXRequestMethod: String{
    case GET = "GET"
    case POST = "POST"
}

class FXNetworkTools: AFHTTPSessionManager {

    // 1. 单例
    static let sharedTool: FXNetworkTools = {
        let instance = FXNetworkTools()
        instance.responseSerializer.acceptableContentTypes?.insert("text/html")
        instance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return instance
    }()
    // 定义请求完成的回调
    typealias FXRequestCallBack = (response: AnyObject?, error: NSError?) -> ()
    
    
    func requset(method: FXRequestMethod = .GET, urlString: String, parameters: AnyObject?, finished:(response: AnyObject?, error: NSError?) ->()){
        if method == .GET{
            GET(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) -> Void in
                    finished(response: responseObject, error: nil)
                }) { (_, error) -> Void in
                    finished(response: nil, error: error)
            }
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) -> Void in
                finished(response: responseObject, error: nil)
                }, failure: { (_, error) -> Void in
                    finished(response: nil, error: error)
            })
        }
    }
}



extension FXNetworkTools {
    func loadStatuses(maxId: Int64, sinceId: Int64,finished:FXRequestCallBack) {
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": FXUserAccountViewModel.sharedViewModel.accessToken!,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        requset(urlString: urlString, parameters: params, finished: finished)
    }
}



